package com.surfilter.msmp.common.util;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class FilePathUtil {
	
	private static String separator = "/";
	
	private static String separator_split = ";";
	
	private static String share_disks = "/fileRoot/snapshot/share_disk_1;/fileRoot/snapshot/share_disk_2;/fileRoot/snapshot/share_disk_3;/fileRoot/snapshot/share_disk_4";
	
	private static List<String> share_disk_q = new ArrayList<String>();
	
	static{
		if(share_disks == null || share_disks.trim().equalsIgnoreCase("")){
			throw new RuntimeException("没有可用分区目录");
		}
		String[] strs = share_disks.split(separator_split);
		share_disk_q.addAll(Arrays.asList(strs));
		if(share_disk_q.size() == 0){
			throw new RuntimeException("没有可用分区目录");
		}
	}
	
	public static String getFilePathByDomain(String dir, String domain){
		String result = null;
		String md5 = MD5Util.MD5(domain);
		int hash = HashUtils.FNVHashPraseLength(md5, 1000);
		String hash_file_path = dir + separator + hash;
		File hash_file = new File(hash_file_path);
		if(!hash_file.exists())hash_file.mkdirs();
		result = hash_file_path + separator + md5;
		return result;
	}
	
	public static String getFilePathByDomainPartion(String domain){
		String result = null;
		String md5 = MD5Util.MD5(domain);
		int hash = HashUtils.FNVHashPraseLength(md5, 1000);
		int file_hash = HashUtils.FNVHashPraseLength(md5, share_disk_q.size());
		String dir = share_disk_q.get(file_hash).trim();
		String hash_file_path = dir + separator + hash;
		File hash_file = new File(hash_file_path);
		if(!hash_file.exists())hash_file.mkdirs();
		result = hash_file_path + separator + md5;
		return result;
	}
	
	public static void main(String[] args) {
		System.out.println(getFilePathByDomainPartion("chinacloud.cn"));
	}

}
